
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;


public class NodePet {
	
	InterfaceFrame frame;
	Player player;
	CustomButton activePet = new CustomButton();
	CustomButton passivePet = new CustomButton();
	CustomButton swap = new CustomButton();
	ReleaseButton release = new ReleaseButton();
	CustomButton lastButton = activePet;
	FillPanel fill = new FillPanel();
	
	NodePet(Player player, InterfaceFrame frame){
		this.frame = frame;
		this.player = player;
	}
	
	public ImageIcon getPic(){
		return new ImageIcon(getClass().getResource(player.activePet.frontImage));
	}
	
	public void updateTextArea(Pet pet){
		
		int attackCount = pet.attacks.size();
		String attacks = "";
		
		for(int i = 0; i < attackCount; i++){
			if(i == attackCount - 1)
				attacks = attacks + new Attack(pet.getAttacks().get(i)).getName();
			else
				attacks = attacks + pet.getAttacks().get(i) + ", ";
		}
		
		frame.nface.textArea.setText(pet.description +
				'\n' +
				'\n' +
				"Typ: " + pet.element + '\n' +
				"Attacker: " + attacks +
				'\n' +
				"Erfarenhet : " + pet.currentXP + "/" + pet.getlevelUpXP() +'\n' +
				"Hälsa : " + pet.getCurrentHealth() + "/" + pet.getMaxHealth() +'\n' +
				"Styrka : " + pet.strength + '\n' +
				"Snabbhet: " + pet.speed  + '\n' +
				"Försvar: " + pet.defense  + '\n' +
				"Storlek: " + pet.size  + '\n');
	}
	
	public void setButtons(){
		
		if(activePet != null)
			frame.nface.popOptions.remove(activePet);
		if(passivePet != null)
			frame.nface.popOptions.remove(passivePet);
		if(swap != null)
			frame.nface.popOptions.remove(swap);
		
		frame.nface.popOptions.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
		activePet.setText("Nivå " + player.activePet.getCurrentLevel() + ": " + player.activePet.getName());
		if(player.passivePet != null)
			passivePet.setText("Nivå " + player.passivePet.getCurrentLevel() + ": " + player.passivePet.getName());
		swap.setText("Byt Plats");
		
		
		activePet.setForeground(Color.BLUE);
		frame.nface.imageLabel.setIcon(getPic());
		updateTextArea(player.activePet);
		
		if(player.passivePet != null){
			frame.nface.add(release, new Integer(5));
			release.addActionListener(new ButtonListener());
			release.addMouseListener(new TheMouseListener());
		}
		
		frame.nface.popOptions.add(activePet);
		if(player.passivePet != null)
			frame.nface.popOptions.add(passivePet);
		frame.nface.popOptions.add(fill);
		if(player.passivePet != null)
			frame.nface.popOptions.add(swap);
		
		activePet.addActionListener(new ButtonListener());
		if(player.passivePet != null)
			passivePet.addActionListener(new ButtonListener());
		swap.addActionListener(new ButtonListener());
		activePet.addMouseListener(new TheMouseListener());
		if(player.passivePet != null)
			passivePet.addMouseListener(new TheMouseListener());
		swap.addMouseListener(new TheMouseListener());
		frame.getContentPane().repaint();
	}
	
	private class ButtonListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
			if(e.getSource().equals(activePet)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = activePet;
				frame.nface.imageLabel.setIcon(new ImageIcon(getClass().getResource(player.activePet.frontImage)));
				frame.nface.textArea.setText(player.activePet.description);
				activePet.setForeground(Color.BLUE);
				updateTextArea(player.activePet);
				frame.getContentPane().repaint();
			}
			else if(e.getSource().equals(passivePet)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = passivePet;
				frame.nface.imageLabel.setIcon(new ImageIcon(getClass().getResource(player.passivePet.frontImage)));
				frame.nface.textArea.setText(player.passivePet.description);
				passivePet.setForeground(Color.BLUE);
				updateTextArea(player.passivePet);
				frame.getContentPane().repaint();
			}
			else if(e.getSource().equals(release)){
				if(lastButton.getText().contains(player.activePet.getName())){
					player.activePet = player.passivePet;
					player.passivePet = null;
					frame.nface.remove(release);
					setButtons();
				}
				else if(lastButton.getText().contains(player.passivePet.getName())){
					player.passivePet = null;
					frame.nface.remove(release);
					setButtons();
				}
			}
			else{
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				player.swapPet();
				activePet.setText("Nivå " + player.activePet.getCurrentLevel() + ": " + player.activePet.getName());
				passivePet.setText("Nivå " + player.passivePet.getCurrentLevel() + ": " + player.passivePet.getName());
				frame.nface.imageLabel.setIcon(new ImageIcon(getClass().getResource(player.activePet.frontImage)));
				activePet.setForeground(Color.BLUE);
				frame.nface.textArea.setText(player.activePet.description);
				lastButton = activePet;
				updateTextArea(player.activePet);
				frame.getContentPane().repaint();
			}
		}
		
	}
	
	private class TheMouseListener implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent e){
		}

		@Override
		public void mouseEntered(MouseEvent e) {
		}

		@Override
		public void mouseExited(MouseEvent e) {
		}
		
		@Override
		public void mousePressed(MouseEvent e) {
			frame.nface.popPrime.show(frame, 50, 55);
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			
		}
		
	}
	
	class CustomButton extends JButton{
		
		private static final long serialVersionUID = 1L;
		
		CustomButton(){
			this.setContentAreaFilled(false);
			this.setForeground(Color.BLACK);
		}
		
		@Override
		public Dimension getMinimumSize(){
			return new Dimension(306,50);	
		}
		@Override
		public Dimension getMaximumSize(){
			return new Dimension(306,50);	
		}
		@Override
		public Dimension getPreferredSize(){
			return new Dimension(306,50);	
		}
		
	}
	
	class ReleaseButton extends JButton{
		
		private static final long serialVersionUID = 1L;
		
		ReleaseButton(){
			this.setContentAreaFilled(false);
			this.setForeground(Color.BLACK);
			this.setText("Gör dig av med husdjuret");
			this.setBounds(930, 550, 200, 50);
		}
		
	}
	
	private class FillPanel extends JPanel{
		
		private static final long serialVersionUID = 1L;
		
		FillPanel(){
			this.setOpaque(false);
		}
		
		@Override
		public Dimension getMinimumSize(){
			return new Dimension(306,200);	
		}
		@Override
		public Dimension getMaximumSize(){
			return new Dimension(306,200);	
		}
		@Override
		public Dimension getPreferredSize(){
			return new Dimension(306,200);	
		}
	}
	
}
